package com.yumeng.common.buss.area.dto;

import com.yumeng.common.buss.area.AreaCodeInfo;
import com.yumeng.common.buss.area.AreaHelper;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

/**
 * @author wxd
 * @date 2022/10/25 15:08
 */
@Getter
@Setter
public class AreaDto {

    public AreaDto(){}
    public AreaDto(String areaCode){
        this(AreaHelper.getAreaCodeInfo(areaCode));
    }

    public AreaDto(AreaCodeInfo codeInfo){
        this.areaCodeCity = codeInfo.getAreaCodeCity();
        this.areaCodeDistrict = codeInfo.getAreaCodeDistrict();
        this.areaCodeSubdistrict = codeInfo.getAreaCodeSubdistrict();
        this.villageCode = codeInfo.getVillageCode();
    }


    @Size(min = 4, max = 4, message = "市级地区编码长度为4")
    @Schema(description = "市级地区编码，长度4")
    private String areaCodeCity;
    @Size(min = 6, max = 6, message = "区级地区编码长度为6")
    @Schema(description = "区级地区编码，长度6")
    private String areaCodeDistrict;
    @Size(min = 9, max = 9, message = "街道级地区编码长度为9")
    @Schema(description = "街道级地区编码，长度9")
    private String areaCodeSubdistrict;
    @Size(min = 12, max = 12, message = "村级地区编码长度为12")
    @Schema(description = "村级地区编码，长度12")
    private String villageCode;

    /**
     * 获取最终地区编码 areaCode
     * 用于参数筛选，都为空，则返回null
     * 不包含最高级编码
     * @return
     */
    @Schema(hidden = true)
    public String getAreaCodeForParam(){
        if (StringUtils.isNotBlank(villageCode)) {
            return villageCode;
        }else if (StringUtils.isNotBlank(areaCodeSubdistrict)) {
            return areaCodeSubdistrict;
        }else if (StringUtils.isNotBlank(areaCodeDistrict)) {
            return areaCodeDistrict;
        }else if (StringUtils.isNotBlank(areaCodeCity)) {
            return areaCodeCity;
        }else{
            return null;
        }
    }

}
